package com.project.myxima.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();
    private SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mDurationFormat = new SimpleDateFormat("mm:ss");
    private ItemClickListener mItemClickListener = null;
    private LongClickListener mLongClickListener;

    @NonNull
    @Override
    public DetailAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_album_detail,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull DetailAdapter.InnerHolder holder, final int position) {
        //找到控件
        View itemView = holder.itemView;
            //顺序的id
        TextView orderTv = itemView.findViewById(R.id.order_text);
            //标题
        TextView titleTv = itemView.findViewById(R.id.trick_title_text);
            //播放次数
        TextView playCount = itemView.findViewById(R.id.track_play_count_text);
            //时长
        TextView durationTv = itemView.findViewById(R.id.detail_item_duration);
            //更新日期
        TextView updateTimeTv = itemView.findViewById(R.id.update_time_text);

        //设置数据
        Track track = mDetailData.get(position);
        orderTv.setText((position+1) + "");
        titleTv.setText(track.getTrackTitle());
        playCount.setText(track.getPlayCount() + "");

        int durationMil = track.getDuration() * 1000;
        String duration = mDurationFormat.format(durationMil);
        durationTv.setText(duration);
        String updateTimeText = mSimpleDateFormat.format(track.getUpdatedAt());
        updateTimeTv.setText(updateTimeText);

        //设置点击事件
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(mDetailData,position);
                }
            }
        });

        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mLongClickListener != null) {
                    mLongClickListener.onLongClick(mDetailData, position);
                }
                return true;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        if (mDetailData != null) {
            mDetailData.clear();
            mDetailData.addAll(tracks);
        }

        notifyDataSetChanged();
    }

    public void setLongClickListener(LongClickListener listener){
        this.mLongClickListener = listener;
    }

    public interface LongClickListener{
        void onLongClick(List<Track> detailData, int position);
    }

    public class InnerHolder extends RecyclerView.ViewHolder{
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public void setItemClickListener(ItemClickListener listener){
        this.mItemClickListener = listener;
    }

    public interface ItemClickListener{
        void onItemClick(List<Track> detailData, int position);
    }
}
